package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcDeviceMapper;
import com.csun.cmny.provider.dao.DmcPropertyManagementMapper;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcPropertyManagement;
import com.csun.cmny.provider.model.dto.propertyManagement.*;
import com.csun.cmny.provider.model.enums.device.DmcDeviceStatusEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementDeviceVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementStausVo;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcPropertyManagementService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DmcPropertyManagementServiceImpl extends BaseService<DmcPropertyManagement> implements DmcPropertyManagementService {
    @Resource
    private DmcPropertyManagementMapper propertyManagementMapper;
    @Resource
    private DmcDeviceService  deviceService;
    @Resource
    private DmcPropertyManagementService propertyManagementService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public int saves(List<DmcPropertyManagement> propertyManagements) {


        return propertyManagementMapper.saves(propertyManagements);
    }

    @Override
    public List<PropertyManagementDeviceVo> selectQuery(ProperManagementSelectDto properManagementSelectDto) {
        return propertyManagementMapper.selectQuery(properManagementSelectDto);
    }

    @Override
    public int deleteManagements(List<Long> deviceIds) {
        return propertyManagementMapper.deleteManagements(deviceIds);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return propertyManagementMapper.selectDeviceData(selectDeviceDataDto);
    }

    @Override
    public int addDeviceAndPropertyManagement(PropertyManagementDeviceSaveDto propertyManagementDeviceSaveDto) {

        //新增设备信息
        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(propertyManagementDeviceSaveDto,dmcDevice);
        dmcDevice.setStatus(DmcDeviceStatusEnum.ENABLE.getKey());

        int save = deviceService.save(dmcDevice);

        //如果设备是网关,redis写入数据
        if (propertyManagementDeviceSaveDto.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex())){
            if (save>0){
                CsunGatewayData csunGatewayData = new CsunGatewayData();
                csunGatewayData.setDeviceId(dmcDevice.getId());
                csunGatewayData.setDeviceNo(dmcDevice.getDeviceNo());

                redisTemplate.opsForValue().set(RedisKeyUtil.getCsunGatewayKey(dmcDevice.getDeviceNo()),
                        csunGatewayData);
            }
        }

        //新增物管信息
        DmcPropertyManagement dmcPropertyManagement = new DmcPropertyManagement();
        BeanUtils.copyProperties(propertyManagementDeviceSaveDto,dmcPropertyManagement);
        dmcPropertyManagement.setDeviceId(dmcDevice.getId());

        dmcPropertyManagement.setManagentNo(propertyManagementDeviceSaveDto.getDeviceNo()+"-"+propertyManagementDeviceSaveDto.getBatch());

        propertyManagementService.save(dmcPropertyManagement);

        return save;
    }

    @Override
    public int updateDevice(PropertyManagementDeviceUpdateDto propertyManagementDeviceUpdateDto) {
        DmcDevice dmcDevice = new DmcDevice();
        dmcDevice.setId(propertyManagementDeviceUpdateDto.getDeviceId());
        BeanUtils.copyProperties(propertyManagementDeviceUpdateDto,dmcDevice);
        int update = deviceService.update(dmcDevice);

        DmcPropertyManagement dmcPropertyManagement = new DmcPropertyManagement();
        BeanUtils.copyProperties(propertyManagementDeviceUpdateDto,dmcPropertyManagement);
        //更新物管编号
        if (PublicUtil.isNotEmpty(propertyManagementDeviceUpdateDto.getDeviceNo())){
            if (PublicUtil.isNotEmpty(propertyManagementDeviceUpdateDto.getBatch())){
                dmcPropertyManagement.setManagentNo(propertyManagementDeviceUpdateDto.getDeviceNo()+"-"+propertyManagementDeviceUpdateDto.getBatch());
            }else {
                DmcPropertyManagement propertyManagement = selectByKey(propertyManagementDeviceUpdateDto.getDeviceId());
                dmcPropertyManagement.setManagentNo(propertyManagementDeviceUpdateDto.getDeviceNo()+"-"+propertyManagement.getBatch());
            }
        }
        propertyManagementService.update(dmcPropertyManagement);
        return update;
    }

    @Override
    public List<DmcPropertyManagement> selectByIds(List<Long> ids) {
        return propertyManagementMapper.selectByIds(ids);
    }

    @Override
    public int delivery(DeviceDeliveryDto deviceDeliveryDto, Date date) {
        return propertyManagementMapper.delivery(deviceDeliveryDto,date);
    }

    @Override
    public List<PropertyManagementStausVo> selectactivateBydiveceIds(List<Long> deviceIds) {
        return propertyManagementMapper.selectactivateBydiveceIds(deviceIds);
    }

    @Override
    public int sendBack(List<Long> deviceIds) {
        return propertyManagementMapper.sendBack(deviceIds);
    }


}
